#include "uanalyserwidget.h"
#include "uanalysercontrols.h"
#include "uanalyserrenderer.h"
#include "CommonDefs.h"

comboNumberItem timeBaseComboItems[] = {
    {QString("5 s"), 5},
    {QString("2 s"), 2},
    {QString("1 s"), 1},
    {QString("0.5 s"), 0.5},
    {QString("0.2 s"), 0.2},
    {QString("0.1 s"), 0.1},
    {QString("0.05 s"), 0.05},
    {QString("0.02 s"), 0.02},
    {QString("0.01 s"), 0.01},
    {QString("5 ms"), 0.005},
    {QString("2 ms"), 0.002},
    {QString("1 ms"), 0.001},
    {QString(), -1}
};

const int defaultTimeBaseIndex = 2;


/*******************************************
 * uAnalyserControls
 *******************************************/

uAnalyserControls::uAnalyserControls(QWidget *parent ) :
    QWidget(parent)
{
    containerLayout = new QVBoxLayout();
    containerLayout->setMargin(0);
    setLayout(containerLayout); //top-level layout for all controls

    channelControlsLayout = new QVBoxLayout();
    channelControlsLayout->setContentsMargins(3,1,3,1);
    channelControlsLayout->setSpacing(0);
    channelControlsLayout->setAlignment(Qt::AlignLeft|Qt::AlignTop);

    QWidget *channelControlsWidget = new QWidget(this);
    channelControlsWidget->setLayout(channelControlsLayout);

    channelControlsScrollArea = new QScrollArea(this);
    channelControlsScrollArea->setWidgetResizable(true);
    channelControlsScrollArea->setFrameStyle(QFrame::NoFrame);
    channelControlsScrollArea->setWidget(channelControlsWidget);
    channelControlsScrollArea->setFixedWidth(210);
    containerLayout->addWidget(channelControlsScrollArea);

    commonControlsGroupBox = new QGroupBox(this);
    commonControlsGroupBox->setTitle(tr("Common settings"));
    commonControlsLayout = new QGridLayout();
    commonControlsLayout->setMargin(0);
    timeBaseLabel = new QLabel("Time base:");
    timeBaseCombo = new QComboBox;
    timeBaseUnitLabel = new QLabel("/div");
    for (int i=0; timeBaseComboItems[i].string != QString(); i++)
        timeBaseCombo->addItem(timeBaseComboItems[i].string, QVariant(timeBaseComboItems[i].value));
    showAxes = new QCheckBox("Show axes");
    showGrid = new QCheckBox("Show grid");

    commonControlsLayout->addWidget(timeBaseLabel, 0, 0, Qt::AlignRight);
    commonControlsLayout->addWidget(timeBaseCombo, 0, 1);
    commonControlsLayout->addWidget(timeBaseUnitLabel, 0, 2, Qt::AlignLeft);
    commonControlsLayout->addWidget(showAxes, 1, 0, 1, 3, Qt::AlignHCenter);
    commonControlsLayout->addWidget(showGrid, 2, 0, 1, 3, Qt::AlignHCenter);
    commonControlsLayout->setSizeConstraint(QLayout::SetMinimumSize);
    commonControlsGroupBox->setLayout(commonControlsLayout);
    containerLayout->addWidget(commonControlsGroupBox);

    mainWidget = qobject_cast<uAnalyserWidget*>(parent);
    connect(timeBaseCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setTimeBase(int)));
    connect(this, SIGNAL(timeBaseChanged(double)), mainWidget, SLOT(setTimeBase(double)));
    //we need the signal to be emited at creation of operator panel, so main widget contains propper
    //time base value
    timeBaseCombo->setCurrentIndex(defaultTimeBaseIndex);

    connect(showAxes, SIGNAL(stateChanged(int)), mainWidget, SLOT(showAxes(int)));
    connect(showGrid, SIGNAL(stateChanged(int)), mainWidget, SLOT(showGrid(int)));
}

//changes time base to make trace as wide as possible(but not wider than 10 grids)
void uAnalyserControls::adjustTimeBase(double time_sweep)
{
    int i;
    for (i=1; timeBaseComboItems[i].string != QString(); i++)
        if (timeBaseComboItems[i].value*10 < time_sweep)
            break;
    timeBaseCombo->setCurrentIndex(i-1);
}

//displays/hides oscilloscope trigger controls (VERY crude functionality right now)
void uAnalyserControls::showTriggerControls(bool show_controls)
{
    if (show_controls)
        {
        triggerControlsGroupBox = new QGroupBox(this);
        triggerControlsGroupBox->setTitle(tr("Trigger settings"));
        triggerControlsLayout = new QGridLayout();
        triggerControlsLayout->setMargin(0);
        triggerToggleButton = new QPushButton(triggerControlsGroupBox);
        triggerControlsLayout->addWidget(triggerToggleButton, 0, 0, Qt::AlignHCenter);
        triggerControlsLayout->setSizeConstraint(QLayout::SetMinimumSize);
        triggerControlsGroupBox->setLayout(triggerControlsLayout);
        containerLayout->insertWidget(1, triggerControlsGroupBox);
        connect(triggerToggleButton, SIGNAL(clicked()), this, SLOT(toggleOscilloscopeState()));
        setOscilloscopeState(false); //we start oscilloscope in pause state
        }
    else
        {
        disconnect(triggerToggleButton, SIGNAL(clicked()), this, SLOT(toggleOscilloscopeState()));
        containerLayout->removeWidget(triggerControlsGroupBox);
        delete triggerToggleButton;
        delete triggerControlsLayout;
        delete triggerControlsGroupBox;
        }
}

void uAnalyserControls::setTimeBase(int i)
{
   emit timeBaseChanged(timeBaseComboItems[i].value);
}

void uAnalyserControls::setOscilloscopeState(bool new_state)
{
    if (new_state) //new_state==true : oscilloscope is running
        triggerToggleButton->setText("Pause");
    else
        triggerToggleButton->setText("Run");
    currentState = new_state;
    mainWidget->pauseOscilloscope(!currentState);
}

